package com.design;

import java.util.List;
import java.util.ArrayList;

import com.design.components.*;
import com.gui.windows.*;
import com.data.*;

/**
 * Singleton Class.
 * This is the class that help to relate all the design components with the rest of the App. Each time an action is made or is intented to 
 * be made to a component, it will be through this class. 
 * */
public class DesignManager {
	
	//**************************************************************************************************
	//	Constructor.
	//**************************************************************************************************
	
	private static DesignManager designManager = null;
	
	private DesignManager() {
		this.actions = new ArrayList <Action> ();
	}
	
	public static DesignManager getDesignManager() {
		if (designManager == null) {
			availableDesignComponentsTypes = new ArrayList <Byte> ();
			availableDesignComponents = new ArrayList <Short> ();
			designManager = new DesignManager();
		}
		return designManager;
	}
	
	
	//**************************************************************************************************
	//	Atributes.
	//**************************************************************************************************
	
	// The availableDesignComponentsTypes list is used to show the components types. A combo box will be bind to it.
	// It will contains all the components types available in the DB.
	// The availableDesignComponents list is used to show the existent components in the current template. A combo box will be bind to it.
	// When a component type is selected by the user in the respcetive comboBox, the componentes shown in the  
	// availableDesignComponents will be filtered & updated. 
	public static List <Byte> availableDesignComponentsTypes; 	// this is the component id prefix.
	public static List <Short> availableDesignComponents;		// this is the component id.
	
	private List <Action> actions = null;
	private Template template;
	
	//**************************************************************************************************
	//	Methods that exports this Class (Public Methods).
	//**************************************************************************************************
	
	public void createNewComponent(byte prefix) {
		switch (prefix) {
			case IdManager.TEMP_PREFIX:
				if (createNewTemplate()) {
					this.template = Template.getTemplate();
					loadReferenceLists();
					MainWindow.getMainWindow().updateObjects(this);
					System.out.println("Template created");
				}	
			break;
			case IdManager.THEM_PREFIX:
				System.out.println("Theme created");
			break;
			default:
				System.out.println("Unknown component");
			break;
		}
	}
	
	public void deleteComponent(short id) {
		
	}
	
	public void editComponent(short id) {
		
	}
	
	//**************************************************************************************************
	//	Inner Methods (Private Methods).
	//**************************************************************************************************
	
	/**
	 * Method that checks if a template was previously created. If true, it promts a message to the user to confirm these action. If the user 
	 * confirms, the method returns true. If not false.
	 * If the was no previoues template it returns true.  
	 * */
	private boolean createNewTemplate() {
		//TODO: Implement this method.
		return true;
	}
	
	/**
	 * It loads from the DB all the available component types, and from the current template all the components.
	 * */
	private void loadReferenceLists() {
		DbImporter.loadAvailableDesignComponentsTypes(availableDesignComponentsTypes);
		if (this.template != null) { // because if the is no template, there are no components. (the template contains all the components)
			bringComponentsAvailableOnTemplate();
		}
	}
	
	private void bringComponentsAvailableOnTemplate() {
		// TODO: Implement this method.
		// Must look around the template, looking for available components and put their id's in the availableDesignComponents list.
		availableDesignComponents.add((short) 001);
		availableDesignComponents.add((short) 010);
		availableDesignComponents.add((short) 011);
		availableDesignComponents.add((short) 012);
	}
	
	//**************************************************************************************************
	//	Inner Classes.
	//**************************************************************************************************
	
	private class Action {
		
		public Action () {}
				
		private byte componentPrefix;
		private String operation; 
	}
	
}
